﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.TextScraping
{
    [Serializable]
    public class PdfDocFullTextInstruction:InstructionBase
    {
        #region props
        private string _SrcConnStr;
        /// <summary></summary>
        public string SrcConnStr
        {
            get
            {
                return this._SrcConnStr;
            }
            set
            {
                this._SrcConnStr = value;
            }
        }

        private string _SelectSql;
        /// <summary></summary>
        public string SelectSql
        {
            get
            {
                return this._SelectSql;
            }
            set
            {
                this._SelectSql = value;
            }
        }

        private string _FilePathField;
        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._FilePathField;
            }
            set
            {
                this._FilePathField = value;
            }
        }

        private string _SrcIDField;
        /// <summary></summary>
        public string SrcIDField
        {
            get
            {
                return this._SrcIDField;
            }
            set
            {
                this._SrcIDField = value;
            }
        }

        private string _TgtConnStr;
        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._TgtConnStr;
            }
            set
            {
                this._TgtConnStr = value;
            }
        }

        private string _TgtTableName;
        /// <summary></summary>
        public string TgtTableName
        {
            get
            {
                return this._TgtTableName;
            }
            set
            {
                this._TgtTableName = value;
            }
        }

        private string _TgtIDField;
        /// <summary></summary>
        public string TgtIDField
        {
            get
            {
                return this._TgtIDField;
            }
            set
            {
                this._TgtIDField = value;
            }
        }

        private string _FullTextField;
        /// <summary></summary>
        public string FullTextField
        {
            get
            {
                return this._FullTextField;
            }
            set
            {
                this._FullTextField = value;
            }
        }

        private string _FlagField;
        /// <summary></summary>
        public string FlagField
        {
            get
            {
                return this._FlagField;
            }
            set
            {
                this._FlagField = value;
            }
        }

        private string _UpdField;
        /// <summary></summary>
        public string UpdField
        {
            get
            {
                return this._UpdField;
            }
            set
            {
                this._UpdField = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfDocFullTextInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcConnStr = string.Empty;
            this._SelectSql = string.Empty;
            this._FilePathField = string.Empty;
            this._SrcIDField = string.Empty;
            this._TgtConnStr = string.Empty;
            this._TgtTableName = string.Empty;
            this._TgtIDField = string.Empty;
            this._FullTextField = string.Empty;
            this._FlagField = string.Empty;
            this._UpdField = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="srcConnStr"></param>
        /// <param name="selectSql"></param>
        /// <param name="filePathField"></param>
        /// <param name="srcIDField"></param>
        /// <param name="tgtConnStr"></param>
        /// <param name="tgtTbl"></param>
        /// <param name="tgtIDField"></param>
        /// <param name="fullTxtField"></param>
        /// <param name="flagField"></param>
        /// <param name="updField"></param>
        public PdfDocFullTextInstruction(Guid componentID, Guid jobTicketID,
            string srcConnStr, string selectSql,
            string filePathField, string srcIDField,
            string tgtConnStr, string tgtTbl,
            string tgtIDField, string fullTxtField,
            string flagField, string updField) 
            : base(componentID, jobTicketID)
        {
            this._SrcConnStr = srcConnStr;
            this._SelectSql = selectSql;
            this._FilePathField = filePathField;
            this._SrcIDField = srcIDField;
            this._TgtConnStr = tgtConnStr;
            this._TgtTableName = tgtTbl;
            this._TgtIDField = tgtIDField;
            this._FullTextField = fullTxtField;
            this._FlagField = flagField;
            this._UpdField = updField;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>serialize settings to xml</summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcConnStr", this.SrcConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSql", this.SelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcIDField", this.SrcIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtTable", this.TgtTableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtIDField", this.TgtIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FullTextField", this.FullTextField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FlagField",this.FlagField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "UpdField", this.UpdField);

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfDocFullTextInstruction instruction = new PdfDocFullTextInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.SrcConnStr = XmlDataUtil.GetAttributeValue(xNode, "SrcConnStr", "");
                instruction.SelectSql = XmlDataUtil.GetAttributeValue(xNode, "SelectSql", "");
                instruction.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                instruction.SrcIDField = XmlDataUtil.GetAttributeValue(xNode, "SrcIDField", "");
                instruction.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                instruction.TgtTableName = XmlDataUtil.GetAttributeValue(xNode, "TgtTable", "");
                instruction.TgtIDField = XmlDataUtil.GetAttributeValue(xNode, "TgtIDField", "");
                instruction.FullTextField = XmlDataUtil.GetAttributeValue(xNode, "FullTextField", "");
                instruction.FlagField = XmlDataUtil.GetAttributeValue(xNode, "FlagField", "");
                instruction.UpdField = XmlDataUtil.GetAttributeValue(xNode, "UpdField", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
